﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyORC
{
    public partial class Form1 : Form
    {

        private string dbPath = "";

        private string Globalapp_id = "17634561";

        private string Globalapi_key = "sCQuGeTfI4GctwIRt7Yho8Aw";

        private string Globalsecret_key = "RxH8Fxs1DV8ob0oT94A8leGjfRLLhPFk";

        private string binPath = Directory.GetCurrentDirectory();

        public Form1()
        {
            InitializeComponent();
            dataGridView1.Visible = false;
            panel2.Visible = false;
        }

        public void getDbPath(string path)
        {
            dbPath = path;
        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            panel2.Visible = false;
            Process.Start("explorer.exe", binPath + "\\helpPdf\\");
            Process.Start(binPath + "\\helpPdf\\身份证识别程序帮助文档_20200116.pdf");
        }

        private void 配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            panel2.Visible = true;
        }

        private void saveKeyButton_Click(object sender, EventArgs e)
        {
            Globalapp_id = APP_IDTextBox.Text.Trim();
            Globalapi_key = API_KeyTextBox.Text.Trim();
            Globalsecret_key = Secret_KeyTextBox.Text.Trim();
            MessageBox.Show("key保存成功");
        }

        private void 查看修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dbPath == "")
            {
                MessageBox.Show("请先选择数据库文件");
                选择数据库ToolStripMenuItem_Click(sender, e);
            }
            if (dbPath == "")
            {
                return;
            }
            
            if (AccessHelper.GetTableName(dbPath).Contains("zdxx"))
            {
                dataGridView1.Visible = true;
                panel2.Visible = false;
                DataTable dt = AccessHelper.getDataTabale(dbPath, "select * from zdxx");
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("没有在该数据库中找到表名为“zdxx”的表！");
            }
        }

        private void 信息识别ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_InfoReg fig = new Frm_InfoReg(Globalapp_id,Globalapi_key,Globalsecret_key);
            fig.TransPath += new transPathHandler(getDbPath);
            fig.ShowDialog();
        }

        private void 选择数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c://";
            openFileDialog.Filter = "数据库文件|*.mdb";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                dbPath = openFileDialog.FileName;
                dataGridView1.Visible = false;
                panel2.Visible = false;
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dbPath == "")
            {
                MessageBox.Show("请先选择数据库文件");
                选择数据库ToolStripMenuItem_Click(sender, e);
            }
            if (dbPath == "")
            {
                return;
            }
            if (AccessHelper.GetTableName(dbPath).Contains("zdxx"))
            {
                Frm_Export Fet = new Frm_Export(dbPath);
                Fet.ShowDialog();
            }
            else
            {
                MessageBox.Show("没有在该数据库中找到表名为“zdxx”的表！");
            }
        }

        private void 清空数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dbPath == "")
            {
                MessageBox.Show("请先选择数据库文件");
                选择数据库ToolStripMenuItem_Click(sender, e);
            }
            if (dbPath == "")
            {
                return;
            }
            if(MessageBox.Show("将要清空该数据库中名为“zdxx”的表中的全部内容，确定吗？", "Confirm Message", MessageBoxButtons.OKCancel)==DialogResult.OK)
            {
                
                if (AccessHelper.GetTableName(dbPath).Contains("zdxx"))
                {
                    AccessHelper.excuteSql(dbPath, "delete * from zdxx");
                    MessageBox.Show("数据库清空完成！");
                }
                else
                {
                    MessageBox.Show("没有在该数据库中找到表名为“zdxx”的表！");
                }

            }
        }
    }
}
